package com.sx.gongzhong.voiceamr;

import android.annotation.SuppressLint;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.util.Log;

import java.io.File;
import java.io.IOException;

public class MediaReocderOpt {

    private static final String LOG_TAG = "MediaRcorderTest";

    private MediaRecorder mRecorder;
    private MediaPlayer mPlayer;
    private String PATH_NAME;

    //初始化
    @SuppressLint("SdCardPath")
    public void Init(String pathName) {
        PATH_NAME = null;
        mPlayer = null;
        mRecorder = null;
        PATH_NAME = pathName;
    }

    //开始录音
    public void StartRecording() {
        Log.e("test", "" + PATH_NAME);
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile(PATH_NAME);
        mRecorder.setAudioSamplingRate(8000);
        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
        mRecorder.start();
    }

    //停止录音
    public void StopRecording() {

        if (mRecorder != null) {
            mRecorder.stop();
            mRecorder.release();
        }

        mRecorder = null;
    }

    //开始播放
    public void StartPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(PATH_NAME);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    //开始播放指定路径的文件,比如点击过暂停的，那么需要合并文件生成新的文件，需要重新指定路径，最后的那个文件名字已经不存在，被删除了
    public void StartPlaying(String path) {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(path);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    //停止播放
    public void StopPlaying() {
        if (mPlayer != null) {
            mPlayer.stop();
            mPlayer.release();
        }
        mPlayer = null;
    }

    //重录
    public void deletePlayingFile() {
        File file = new File(PATH_NAME);

        if (file.exists()) { // 判断是否存在
            try {
                file.delete();
                Log.e("test", "重录删除文件成功");
            } catch (Exception e) {
                Log.e("test", "重录删除文件失败");
            }
        }
    }

    //重录
    public void deletePlayingFile(String path) {
        File file = new File(path);

        if (file.exists()) { // 判断是否存在
            try {
                file.delete();
                Log.e("test", "重录删除文件成功");
            } catch (Exception e) {
                Log.e("test", "重录删除文件失败");
            }
        }
    }

    //是否播放结束
    public boolean IsStopPlaying() {
        return !mPlayer.isPlaying();
    }

    //释放资源
    public void Destory() {
        StopRecording();
        StopPlaying();
    }
}
